package com.adong.crowd.service.impl;

import com.adong.crowd.entity.Role;
import com.adong.crowd.mapper.AssignMapper;
import com.adong.crowd.service.api.AssignService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;


@Service
public class AssignServiceImpl implements AssignService {

    @Autowired
    private AssignMapper assignMapper;

    /**
     * 根据adminId查询分配的角色
     *
     * @param adminId
     * @return
     */
    @Override
    public List<Role> getAssignedRole(Integer adminId) {
        return assignMapper.selectAssignedRole(adminId);
    }

    /**
     * 查询adminId为分配的角色
     *
     * @param adminId
     * @return
     */
    @Override
    public List<Role> getUnAssignedRole(Integer adminId) {
        return assignMapper.selectUnAssignedRole(adminId);
    }

    /**
     * 更新用户的角色信息
     *
     * @param adminId
     * @param roleIdList
     */
    @Override
    public void saveAdminRoleRelationship(Integer adminId, List<Integer> roleIdList) {
        /**
         * 旧
         * adminId  roleId
         *  1       1       (删除)
         *  1       2       (删除)
         *  1       3
         *  ----------------------
         *  新
         *  adminId roleId
         *  1       3
         *  1       4       (添加)
         *  1       5       (添加)
         * 不好更新,于是我们可以删除全部旧数据,保存roleIdList保存全部的新的数据
         */
        assignMapper.deleteOLdRelationship(adminId);

        /**
         * 保存新的关联关系
         */
        if (roleIdList != null && roleIdList.size() > 0) {
            assignMapper.insertNewRelationship(adminId, roleIdList);
        }
    }
}
